package book_management.book;

import java.util.ArrayList;
import java.util.List;

/**
 * 书架
 */

public class BookList {

    // 书架就是保存书籍的，书本的个数不确定，要么用动态数组，要么使用链表
    private List<Book> books = new ArrayList<>();

    // 保存当前书架上所有的书籍名称，以后的操作就是根据这个书籍的名称来查找具体的Book对象，
    // 一一对应的映射关系
    private List<String> booksName = new ArrayList<>();

    public BookList(){
        // 产生一个书架对象时，默认上架的四本书籍
        books.add(new Book("三国演义", "罗贯中", 66.6, "小说"));
        books.add(new Book("水浒传", "施耐庵", 55.6, "小说"));
        books.add(new Book("西游记", "吴承恩", 44.6, "小说"));
        books.add(new Book("红楼梦", "曹雪芹&未知", 88.6, "小说"));
        // 将当前这四本书的书名也添加到booksName列表中
        booksName.add("三国演义");
        booksName.add("水浒传");
        booksName.add("西游记");
        booksName.add("红楼梦");
    }

    public void listBooks() {
        for(Book book : books){
            System.out.println(book);
        }
    }

    public boolean contains(String booksName) {
        // 遍历booksName这个列表即可
        for (String str : this.booksName) {
            if (str.equals(booksName)) {
                return true;
            }
        }

        return false;
    }

    public void addNewBook(String booksName, String author, String type, double price) {
       Book book = new Book(booksName, author, price, type);
       this.books.add(book);//添加一本具体的书
       this.booksName.add(booksName);//把书的名字添加的为它创建的数组中

    }

    public void getBook(String nameBook) {
        int i = 0;
        for(String str : this.booksName){
            if(str.equals(nameBook)) {
                System.out.println(this.books.get(i));
            }
            i++;
        }
    }

    public void BorrowBook(String nameBook) {
        int i = 0;
        for(String str : this.booksName){
            if(str.equals(nameBook)) {
                this.books.get(i).setBorrowed(true);
            }
            i++;
        }
    }

    public void ReturnBook(String nameBook) {
        int i = 0;
        for(String str : this.booksName){
            if(str.equals(nameBook)) {
                this.books.get(i).setBorrowed(false);
            }
            i++;
        }
    }

    public void RemoveBook(String nameBook) {
        int i = 0;
        for(String str : this.booksName){

            if(str.equals(nameBook)) {
                this.books.remove(i);
                this.booksName.remove(str);
            }
            i++;
        }
    }


    public void UpdateBook(String nameBook, double price, String type, int isBorrowed) {
        int i = 0;
        for(String str : this.booksName){
            if(str.equals(nameBook)) {
                this.books.get(i).setPrice(price);
                this.books.get(i).setType(type);
                if(isBorrowed == 0) {
                    this.books.get(i).setBorrowed(true);
                }else{
                    this.books.get(i).setBorrowed(false);
                }
            }
            i++;
        }
    }
}
